﻿//https://leetcode.cn/problems/intersection-of-two-linked-lists/?envType=study-plan-v2&envId=top-100-liked

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* getIntersectionNode(ListNode* headA, ListNode* headB)
    {
        if (headA == nullptr || headB == nullptr)
        {
            return nullptr;
        }
        ListNode* cur1 = headA;
        ListNode* cur2 = headB;

        while (cur1 != cur2)
        {
            cur1 = cur1 == nullptr ? headB : cur1->next;
            cur2 = cur2 == nullptr ? headA : cur2->next;
        }

        return cur1;

    }
};